
var id="";
readercourse();//课程列表
courselist();//全部数据

function readercourse(){
  $.ajax({
    url:"/api/coursetypes",
    method:"get",
    success:function(data){
        coursetypes(data);
    }
  })

  function coursetypes(data) {
    var $fragment = $(document.createDocumentFragment())
    $.each(data.data, function(index, item) {
      if (index === 0) {
        var $courseTypeAll = $('<li class="uk-active"><a>'+i18next.t('home.all')+'</a></li>').appendTo($fragment)
        $courseTypeAll.on('click', function() {
          $(this).addClass('uk-active').siblings().removeClass('uk-active')
          courselist();
        })
      }
      var $courseType = $('<li><a>' + item.coursetypename + '</a></li>').appendTo($fragment)
      $courseType.on('click', function() {
        $(this).addClass('uk-active').siblings().removeClass('uk-active')
        courseItermlist(item.coursetypeuid);
      })
    })
    $fragment.appendTo($('#J_coursetypes'))
  }
}

//每个科目的数据
function courseItermlist(coursetypeuid){
  $.ajax({
    url:"/api/products?status=2",
    data:{
      coursetype :coursetypeuid
    },
    method:"get",
    success:function(data){
        products(data)
    }
  })
}
//全部的数据
function courselist(){
  $.ajax({
    url:"/api/products?status=2",
    method:"get",
    success:function(data){
        products(data);
    }
  })

}
function loadCourses(data) {
  if(!data.length) {
    $('#J_courses').hide()
    $('#j-course-empty').show().text(i18next.t('home.nodatas'))//暂无数据
    return;
  }
  var htmlStr = ''
  $.each(data, function (i, item) {

    htmlStr += '<a href="course.html?id='+ item.id +'" class="course">\
      <div class="uk-thumbnail">\
        <img class="lazy" data-original="'+ item.cover +'" src="/home/img/lazyload_course.png">\
        <h3 class="uk-text-truncate">'+ item.title +'</h3>\
        <div>\
          <span>教师：<span>'+ (item.masterinfo ? item.masterinfo.lastname : '') +'</span></span>\
          <span class="price">'+ (item.price === '0.00'? i18next.t('home.free'):('￥'+item.price)) +'</span>\
        </div>\
        <div class="uk-clearfix" style="margin-top: 8px;">\
          <div class="uk-float-left detail">\
             '+i18next.t('home.browse')+'：<span class="visit">'+ (item.visited||0) +'</span>'+i18next.t('home.buy')+'：<span class="buy">'+ (item.sold||0) +'</span>\
          </div>\
          <button class="uk-button uk-button-primary uk-button-small uk-float-right">'+i18next.t('home.buy')+'</button>\
        </div>\
      </div>\
    </a>'
  })

  $('#j-course-empty').hide()
  $('#J_courses').show().html(htmlStr)

  imgLazyload()
}




//渲染批量数据
function  products(data){
  loadCourses(data.data)
}
